/**
 * Created by MetaLab_K on 2016/3/31.
 */
'use strict';
angular.module('metalabPartnerApp').controller('DemandListAllCtrl', ['$scope', '$stateParams', '$state', 'MetaResources', 'MetaSEO', 'MetaRemind',
    function ($scope, $stateParams, $state, resource, MetaSEO, MetaRemind) {
        $scope.waiting = true;//等待标志

        if ($state.is('meta.demand.list.all')) {
            $scope.tabs[0].active = true;
        } else if ($state.is('meta.demand.list.released')) {
            $scope.tabs[1].active = true;
        }

        ///**数据模板
        $scope.page = {
            currentPage: parseInt($stateParams.page),
            data: [],
            total: 0,
            pageNumber: 10
        };

        /**
         * @function 请求新的数据
         * @param page 跳转到第几页
         * @param area 筛选领域
         */
        var changeData = function (page, area, keywords) {
            $scope.waiting = true;
            if (keywords) {
                $scope.waitingPromise = resource.search.website({
                    areas: area,
                    type: "demand",
                    _range: ((parseInt(page) - 1) * $scope.page.pageNumber + 1) + '_' + parseInt(page) * $scope.page.pageNumber,
                    _sort: "-rank,-updatedAt",
                    version: "online",
                    online: "true",
                    verified: "true",
                    searchText: keywords
                }, function (result, headers) {
                    $scope.waiting = false;
                    $scope.page.total = headers()['x-count'] ? parseInt(headers()['x-count']) : 0;
                    $scope.page.data = result;
                }, function (err) {
                    $scope.waiting = false;
                    $scope.page.total = 0;
                    $scope.page.data = [];
                }).$promise;
            } else {
                $scope.waitingPromise = resource.demand.query({
                    areas: area,
                    _range: ((parseInt(page) - 1) * $scope.page.pageNumber + 1) + '_' + parseInt(page) * $scope.page.pageNumber,
                    version: 'online',
                    online: true,
                    verified: true,
                    _sort: '-rank,-updatedAt'
                }, function (data, headers) {
                    $scope.waiting = false;
                    $scope.page.total = headers()['x-count'] ? parseInt(headers()['x-count']) : 0;
                    $scope.page.data = data;
                }, function (err) {
                    $scope.waiting = false;
                    MetaRemind.error('error:', err);
                }).$promise;
            }
        };

        /**
         * @function 请求数据
         */
        $scope.filters[0].config.chose = $stateParams.area || '不限';
        $scope.$parent.searchText = $stateParams.keywords;
        MetaSEO.setSEO('迈科技-企业技术需求-产业科技难题-征集解决方案',
            '迈科技-集中展示急需解决的、真实可靠的企业需求，任何人可以免费获取需求方的联系信息，并选择您擅长的领域，主动推广自己的技术', $stateParams.areas + ',' + $stateParams.keywords);
        changeData($stateParams.page, $stateParams.area, $stateParams.keywords);
    }]);
